﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BbsbWebService.Classes;
using BbsbWebService.Repositories;
using BbsbWebService.Enums;
using BbsbWebService;
using System.Collections.ObjectModel;

namespace BbsbWsGui
{
    /// <summary>
    /// Interaction logic for ItemsTab.xaml
    /// </summary>
    public partial class ItemsTab : UserControl
    {
        List<ControllableItem> items;
        public ItemsTab()
        {
            InitializeComponent();
            
        }

        #region Items Methods

        private void removeItemBtn_Click(object sender, RoutedEventArgs e)
        {
            ControllableItem item = SelectedItem;
            if (item != null)
            {
                RepositoryInstances.ItemRepository.RemoveItem(item);
                //RefreshItems();
            }
        }

        private void addItemBtn_Click(object sender, RoutedEventArgs e)
        {
            AddItemWindow diag = new AddItemWindow();
            if (diag.ShowDialog() == true &&
                diag.Item != null)
            {
                RepositoryInstances.ItemRepository.SaveItem(diag.Item);
                //RefreshItems();
            }
        }

        private void editItemBtn_Click(object sender, RoutedEventArgs e)
        {
            EditSelectedItem();
        }

        private void testOnBtn_Click(object sender, RoutedEventArgs e)
        {
            ControllableItem item = SelectedItem;
            if (item != null)
            {
                RepositoryInstances.ItemRepository.SendPower(item.Id, PowerOption.On);
                //RefreshItems();
            }
        }

        private void testOffBtn_Click(object sender, RoutedEventArgs e)
        {
            ControllableItem item = SelectedItem;
            if (item != null)
            {
                RepositoryInstances.ItemRepository.SendPower(item.Id, PowerOption.Off);
                //RefreshItems();
            }
        }

        private void toggleButton_Click(object sender, RoutedEventArgs e)
        {
            ControllableItem item = SelectedItem;
            if (item != null)
            {
                RepositoryInstances.ItemRepository.Toggle(item.Id);
                //RefreshItems();
            }
        }

        private void refreshList_Click(object sender, RoutedEventArgs e)
        {
            RepositoryInstances.ItemRepository.Refresh();
            //RefreshItems();
        }

        #endregion

        public bool Enabled
        {
            set
            {
                bool enabled = value;
                addItemBtn.IsEnabled = enabled;
                editItemBtn.IsEnabled = enabled;
                removeItemBtn.IsEnabled = enabled;

                allItemsLst.IsEnabled = enabled;
                testOffBtn.IsEnabled = enabled;
                testOnBtn.IsEnabled = enabled;
                toggleButton.IsEnabled = enabled;
                refreshList.IsEnabled = enabled;
            }
        }

        public ObservableCollection<ControllableItem> ItemsSource
        {
            set
            {
                allItemsLst.ItemsSource = value;
                items = value.ToList();
            }
        }

        private void allItemsLst_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            EditSelectedItem();
        }

        private void EditSelectedItem()
        {
            ControllableItem item = SelectedItem;
            if (item != null)
            {
                AddItemWindow diag = new AddItemWindow(item);
                if (diag.ShowDialog() == true &&
                    diag.Item != null)
                {
                    RepositoryInstances.ItemRepository.SaveItem(diag.Item);
                    //RefreshItems();

                    // We have just updated the item so it won't properly be selected
                    allItemsLst.SelectedItem = diag.Item;
                }
            }
        }
        
        private ControllableItem SelectedItem
        {
            get
            {
                return allItemsLst.SelectedItem as ControllableItem;
            }
        }
    }
}
